package fanshe1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Testperson {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<person> personClass = person.class;//通过反射将字节码反编译为源码
        Constructor<?>[] constructors = personClass.getConstructors();//找到所有的构造器
        System.out.println("constructors = " + constructors);
        Constructor<person> constructor = personClass.getConstructor(String.class,Integer.class);//找到专一构造器（通过类型）
        person xiaowang = constructor.newInstance("小王", 20);//在构造器里创建新的对象
        Method sayHello = personClass.getMethod("sayHello");//找到方法（通过方法名）
        sayHello.invoke(xiaowang);//将新的对象放到方法中并输出



    }
}
